<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>

    <div class="component-style">
      <p class="head-title">标签样式</p>
      <el-form :model="form" label-position="left" label-width="auto">
        <el-form-item label="分类标题颜色">
          <el-color-picker v-model="form.tabStyle.titleColor" />
          <el-input style="width: 160px; margin-left: 10px" v-model="form.tabStyle.titleColor">
            <template #append>
              <el-button @click="form.tabStyle.titleColor = '#333333'">重置</el-button>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="未选中标题颜色">
          <el-color-picker v-model="form.tabStyle.inactiveTitleColor" />
          <el-input
            style="width: 160px; margin-left: 10px"
            v-model="form.tabStyle.inactiveTitleColor"
          >
            <template #append>
              <el-button @click="form.tabStyle.inactiveTitleColor = '#999999'">重置</el-button>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="下划线颜色">
          <el-color-picker v-model="form.tabStyle.underlineColor" />
          <el-input style="width: 160px; margin-left: 10px" v-model="form.tabStyle.underlineColor">
            <template #append>
              <el-button @click="form.tabStyle.underlineColor = '#ff4d4f'">重置</el-button>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="标签文字大小">
          <div class="flex">
            <el-slider style="width: 70%" v-model="form.tabStyle.tabFontSize" :min="12" :max="30" />
            <el-input
              type="number"
              style="width: 30%; margin-left: 20px"
              v-model.number="form.tabStyle.tabFontSize"
              :min="12"
              :max="30"
            >
              <template #append>px</template>
            </el-input>
          </div>
        </el-form-item>

        <!--        <el-form-item label="内容背景颜色">-->
        <!--          <el-color-picker v-model="form.tabStyle.backgroundColor" />-->
        <!--          <el-input style="width: 160px; margin-left: 10px" v-model="form.tabStyle.backgroundColor">-->
        <!--            <template #append>-->
        <!--              <el-button @click="form.tabStyle.backgroundColor = '#ffffff'">重置</el-button>-->
        <!--            </template>-->
        <!--          </el-input>-->
        <!--        </el-form-item>-->
        <el-form-item label="广告图片">
          <div>
            <div class="avatar-uploader" @click="handleChangeImage">
              <el-image
                v-if="showComponentData.linkImage"
                :src="showComponentData.linkImage"
                class="avatar"
                fit="cover"
              ></el-image>
              <i v-else class="el-icon-plus avatar-uploader-icon"
                ><el-icon> <Plus /> </el-icon
              ></i>
              <div v-if="showComponentData.linkImage" class="mask">替换</div>
              <el-icon
                v-if="showComponentData.linkImage"
                class="del-image"
                :size="20"
                @click.stop="handleDelImg"
                ><CircleCloseFilled
              /></el-icon>
            </div>

            <div class="column">
              <div class="link">
                <el-icon style="color: var(--el-color-primary)"><Link /></el-icon>
                <el-link type="primary" @click="showLinkDataDialog = true">{{
                  showComponentData.link?.name || '请选择跳转链接'
                }}</el-link></div
              >
            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      @close-dialog="handleCloseLinkDialog"
    ></link-list-dialog>

    <image-selected-dialog
      v-if="showImageChooseDialog"
      :single-type="true"
      :operation-type="false"
      :show-image-choose-dialog="showImageChooseDialog"
      @close-dialog="handleCloseImageDialog"
    ></image-selected-dialog>
  </div>
</template>

<script setup lang="ts">
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import debounce from 'lodash/debounce'
  import { CircleCloseFilled, Link, Plus } from '@element-plus/icons-vue'
  import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'

  const props = defineProps({
    contentData: {
      type: Object,
      default() {
        return {}
      }
    }
  })

  const form = reactive({
    tabStyle: {
      titleColor: '#333333',
      inactiveTitleColor: '#999999',
      underlineColor: '#03c36b',
      backgroundColor: '#ffffff',
      tabFontSize: 18
    }
  })
  const showLinkDataDialog = ref(false)
  const showImageChooseDialog = ref(false)

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.tabStyle = raw.tabStyle
    handleUpdateComponent()
  }, 300)

  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  function handleChangeImage() {
    showImageChooseDialog.value = true
  }

  function handleDelImg() {
    showComponentData.value.linkImage = ''
  }

  function handleCloseLinkDialog(value: any) {
    if (value) {
      showComponentData.value.link = value
    }
    showLinkDataDialog.value = false
  }

  function handleCloseImageDialog(value: any) {
    if (value) {
      showComponentData.value.linkImage = value.fileUrl
      handleUpdateComponent()
    }
    showImageChooseDialog.value = false
  }

  onMounted(() => {
    if (Object.keys(showComponentData.value.tabStyle).length !== 0) {
      form.tabStyle = showComponentData.value.tabStyle
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .head-title {
      padding: 20px 0;
      font-size: 14px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .component-style {
      width: 100%;
      box-sizing: border-box;
      margin-top: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .title {
          font-size: 16px;
          font-weight: bold;
        }
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }
    }

    .avatar-uploader {
      border: 1px dashed #d9d9d9;
      border-radius: 12px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      width: 80px;
      height: 80px;
      background-color: #fff;
      text-align: center;
      margin-bottom: 10px;

      .mask {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 24px;
        background-color: rgba(0, 0, 0, 0.6);
        display: flex;
        align-items: center;
        justify-content: center;
        opacity: 0;
        transition: opacity 0.3s ease;
        color: #fff;
        font-size: 12px;
      }

      .del-image {
        display: none;
        position: absolute;
        top: 0;
        right: 0;
        cursor: pointer;
        color: #8c939d;
      }

      &:hover .mask {
        opacity: 1;
      }

      &:hover .del-image {
        display: block;
      }
    }

    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 80px;
      height: 80px;
      line-height: 80px;
      text-align: center;
    }
  }
</style>
